#include "meun.h"
#include "C_H.h"

// 按键定义
#define KEY_UP      "/dev/zf_driver_gpio_key_0"    // 上键
#define KEY_DOWN    "/dev/zf_driver_gpio_key_1"    // 下键
#define KEY_ENTER   "/dev/zf_driver_gpio_key_2"    // 确认键
#define KEY_BACK    "/dev/zf_driver_gpio_key_3"    // 返回键

// 全局变量
static uint8_t func_index = 0;          // 当前功能索引
static uint8_t menu_flag = 0;           // 菜单标志
static uint8_t key_status_last = 1;     // 上一次按键状态
static uint8_t key_up_last = 1;
static uint8_t key_down_last = 1;
static uint8_t key_enter_last = 1;
static uint8_t key_back_last = 1;
static int PID_num_jiantou = 10;
static int x_zuobiao = 20;
static int x_zuobiao_two = 88;
bool start_flag = false;  // 起跑标志位初始化为false
static uint8_t start_menu_flag = 0;  // 起跑菜单标志
void (*current_operation_index)(void);

// 定义不同的菜单表
Menu_table table_PID[25] = {
    {0, 0, 1, 0, meun_fun0},      // 主菜单
    {1, 2, 5, 0, meun_fun1},      // 舵机PID P项
    {2, 3, 7, 0, meun_fun2},      // 舵机速度
    {3, 4, 9, 0, meun_fun3},      // 舵机PID D项
    {4, 1, 11, 0, meun_fun4},     // 圆环减速度

    {5, 6, 5, 1, fun2},           // 舵机PID P项 +
    {6, 5, 6, 1, fun5},           // 舵机PID P项 -

    {7, 8, 7, 2, fun3},           // 舵机速度 +
    {8, 7, 8, 2, fun6},           // 舵机速度 -

    {9, 10, 9, 3, fun4},          // 舵机PID D项 +
    {10, 9, 10, 3, fun7},         // 舵机PID D项 -

    {11, 11, 11, 4, meun_fun5},   // 圆环减速度 -
};

Menu_table table_image[25] = {
    {0, 0, 1, 0, meun_fun0_image},    // 图像主菜单
    {1, 2, 0, 0, fun1_image},         // 图像显示1
    {2, 1, 0, 0, fun2_image},         // 图像显示2
};

Menu_table table_motor[25] = {
    {0, 0, 1, 0, meun_fun0_motor},    // 电机主菜单
    {1, 2, 7, 0, fun1_motor},         // 左电机P +
    {2, 3, 8, 0, fun2_motor},         // 左电机I +
    {3, 4, 9, 0, fun3_motor},         // 右电机P +
    {4, 5, 10, 0, fun4_motor},        // 右电机I +
    {5, 6, 11, 0, fun9_motor},        // 差速系数 +
    {6, 1, 12, 0, fun11_motor},       // 一级一级的加的比例 +

    {7, 7, 7, 1, fun5_motor},         // 左电机P -
    {8, 8, 8, 2, fun6_motor},         // 左电机I -
    {9, 9, 9, 3, fun7_motor},         // 右电机P -
    {10, 10, 10, 4, fun8_motor},      // 右电机I -
    {11, 11, 11, 5, fun10_motor},     // 差速系数 -
    {12, 12, 12, 6, fun12_motor},     // 一级一级的加的比例 -
};

Menu_table table_car_start[25] = {
    {0, 0, 1, 0, meun_fun0_car_go},   // 起跑主菜单
    {1, 2, 1, 0, fun1_car_go},        // 小车启动
    {2, 3, 5, 0, fun2_car_go},        // 设置2
    {3, 4, 3, 0, fun3_car_go},        // 设置3
    {4, 1, 6, 0, fun4_car_go},        // 设置4
    {5, 5, 5, 2, fun5_car_go},        // 减小设置
    {6, 7, 6, 4, fun6_car_go},        // 增加设置1
    {7, 6, 7, 4, fun7_car_go},        // 增加设置2
};

// 按键处理
void Menu_key_set(void) {
    uint8_t key_up = gpio_get_level(KEY_UP);
    uint8_t key_down = gpio_get_level(KEY_DOWN);
    uint8_t key_enter = gpio_get_level(KEY_ENTER);
    uint8_t key_back = gpio_get_level(KEY_BACK);

    // 在主界面时（func_index == 0），使用向下键切换菜单
    if (func_index == 0 && !key_down && key_down_last) {
        menu_flag++;
        if (menu_flag > 3) menu_flag = 0;
        ips200_clear();
        system_delay_ms(10);
    }

    // 根据当前菜单标志处理不同菜单的逻辑
    if (menu_flag == 0) {  // PID菜单
        if (!key_up && key_up_last) {
            if (func_index >= 5 && func_index <= 7) {
                table_PID[func_index].current_operation();
            } else {
                ips200_clear();
                system_delay_ms(10);
                func_index = table_PID[func_index].next;
            }
        } else if (!key_down && key_down_last) {
            if (func_index >= 5 && func_index <= 7) {
                // 可添加向下按键在特定项的处理逻辑
            } else {
                ips200_clear();
                system_delay_ms(10);
                func_index = table_PID[func_index].prev;
            }
        } else if (!key_enter && key_enter_last) {
            // 确认键处理逻辑
        } else if (!key_back && key_back_last) {
            if (func_index != 0) {
                ips200_clear();
                system_delay_ms(10);
                func_index = 0;
            }
        }
        current_operation_index = table_PID[func_index].current_operation;
    }
    else if (menu_flag == 1) {  // 图像菜单
        if (!key_up && key_up_last) {
            ips200_clear();
            system_delay_ms(10);
            func_index = table_image[func_index].next;
        } else if (!key_down && key_down_last) {
            ips200_clear();
            system_delay_ms(10);
            func_index = table_image[func_index].prev;
        } else if (!key_enter && key_enter_last) {
            // 确认键处理逻辑
        } else if (!key_back && key_back_last) {
            if (func_index != 0) {
                ips200_clear();
                system_delay_ms(10);
                func_index = 0;
            }
        }
        current_operation_index = table_image[func_index].current_operation;
    }
    else if (menu_flag == 2) {  // 电机菜单
        if (!key_up && key_up_last) {
            ips200_clear();
            system_delay_ms(10);
            func_index = table_motor[func_index].next;
        } else if (!key_down && key_down_last) {
            ips200_clear();
            system_delay_ms(10);
            func_index = table_motor[func_index].prev;
        } else if (!key_enter && key_enter_last) {
            // 确认键处理逻辑
        } else if (!key_back && key_back_last) {
            if (func_index != 0) {
                ips200_clear();
                system_delay_ms(10);
                func_index = 0;
            }
        }
        current_operation_index = table_motor[func_index].current_operation;
    }
    else if (menu_flag == 3) {  // 起跑菜单
        if (!key_up && key_up_last) {
            ips200_clear();
            system_delay_ms(10);
            func_index = table_car_start[func_index].next;
        } else if (!key_down && key_down_last) {
            ips200_clear();
            system_delay_ms(10);
            func_index = table_car_start[func_index].prev;
        } else if (!key_enter && key_enter_last) {
            if (func_index == 1) {
                start_flag = true;
            }
        } else if (!key_back && key_back_last) {
            if (func_index != 0) {
                ips200_clear();
                system_delay_ms(10);
                func_index = 0;
            }
        }
        current_operation_index = table_car_start[func_index].current_operation;
    }

    // 执行当前菜单项操作
    if (current_operation_index != NULL) {
        current_operation_index();
    }

    // 更新按键状态
    key_up_last = key_up;
    key_down_last = key_down;
    key_enter_last = key_enter;
    key_back_last = key_back;
}

// 其他可能缺少的函数示例，可按需修改
void meun_fun0(void) {
    ips200_show_string(30, 20, "Main PID Menu");
}

// 菜单初始化
void Menu_Init(void) {
    // 清屏
    ips200_clear();
    func_index = 0;
    menu_flag = 0;
}

// 图像菜单函数
void meun_fun0_image(void) {
    if(!start_flag) {  // 如果没有起跑，显示图像和参数
        // 显示图像
        ips200_show_gray_image(0, 0, Pixle[0], LCDW, LCDH);
    } else {
        // 如果已经起跑，显示运行状态
        ips200_clear();
        ips200_show_string(30, 60, "Running...");
    }
}

// 舵机PID菜单函数
void meun_fun1(void) {
    ips200_show_string(x_zuobiao, 40, "P:");
    ips200_show_float(x_zuobiao_two, 40, pid.Kp, 2, 2);
    ips200_show_string(x_zuobiao, 60, "Speed");
    ips200_show_int(x_zuobiao_two, 60, pid.Ki, 3);
    ips200_show_string(x_zuobiao, 80, "D:");
    ips200_show_float(x_zuobiao_two, 80, pid.Kd, 2, 2);
    ips200_show_string(0, 40, "->");
}

void meun_fun2(void) {
    ips200_show_string(35, 20, "Servo Speed Menu");
    ips200_show_string(x_zuobiao, 40, "Speed:");
    ips200_show_int(x_zuobiao_two, 40, Speed_Goal, 3);
    ips200_show_string(0, 40, "->");
}

void meun_fun3(void) {
    ips200_show_string(35, 20, "Servo PID D Menu");
    ips200_show_string(x_zuobiao, 40, "D:");
    ips200_show_float(x_zuobiao_two, 40, pid.Kd, 2, 2);
    ips200_show_string(0, 40, "->");
}

void meun_fun4(void) {
    ips200_show_string(35, 20, "Ring Deceleration Menu");
    ips200_show_string(x_zuobiao, 40, "Deceleration:");
    ips200_show_string(0, 40, "->");
}

void meun_fun5(void) {
    ips200_show_string(35, 20, "Ring Deceleration - Menu");
    // 可添加具体显示逻辑
}

void fun2(void) {
    Adjust_zhi(0);
    meun_fun1();
}

void fun3(void) {
    Adjust_wan();
    meun_fun2();
}

void fun4(void) {
    Adjust_minn();
    meun_fun3();
}

void fun5(void) {
    Adjust_zhi_(0);
    meun_fun1();
}

void fun6(void) {
    Adjust_wan_();
    meun_fun2();
}

void fun7(void) {
    Adjust_minn_();
    meun_fun3();
}

void fun1_image(void) {
    ips200_show_string(30, 20, "Image Display 1");
    // 可添加图像显示1的具体逻辑
}

void fun2_image(void) {
    ips200_show_string(30, 20, "Image Display 2");
    // 可添加图像显示2的具体逻辑
}

void meun_fun0_motor(void) {
    ips200_show_string(30, 20, "Motor Main Menu");
}

void fun1_motor(void) {
    ips200_show_string(30, 20, "Left Motor P +");
    // 可添加左电机P + 的具体逻辑
}

void fun2_motor(void) {
    ips200_show_string(30, 20, "Left Motor I +");
    // 可添加左电机I + 的具体逻辑
}

void fun3_motor(void) {
    ips200_show_string(30, 20, "Right Motor P +");
    // 可添加右电机P + 的具体逻辑
}

void fun4_motor(void) {
    ips200_show_string(30, 20, "Right Motor I +");
    // 可添加右电机I + 的具体逻辑
}

void fun5_motor(void) {
    ips200_show_string(30, 20, "Left Motor P -");
    // 可添加左电机P - 的具体逻辑
}

void fun6_motor(void) {
    ips200_show_string(30, 20, "Left Motor I -");
    // 可添加左电机I - 的具体逻辑
}

void fun7_motor(void) {
    ips200_show_string(30, 20, "Right Motor P -");
    // 可添加右电机P - 的具体逻辑
}

void fun8_motor(void) {
    ips200_show_string(30, 20, "Right Motor I -");
    // 可添加右电机I - 的具体逻辑
}

void fun9_motor(void) {
    ips200_show_string(30, 20, "Differential Coefficient +");
    // 可添加差速系数 + 的具体逻辑
}

void fun10_motor(void) {
    ips200_show_string(30, 20, "Differential Coefficient -");
    // 可添加差速系数 - 的具体逻辑
}

void fun11_motor(void) {
    ips200_show_string(30, 20, "Incremental Ratio +");
    // 可添加一级一级的加的比例 + 的具体逻辑
}

void fun12_motor(void) {
    ips200_show_string(30, 20, "Incremental Ratio -");
    // 可添加一级一级的加的比例 - 的具体逻辑
}

void meun_fun0_car_go(void) {
    ips200_show_string(30, 20, "Car Start Main Menu");
}

void fun1_car_go(void) {
    ips200_show_string(30, 20, "Car Start");
    // 可添加小车启动的具体逻辑
}

void fun2_car_go(void) {
    ips200_show_string(30, 20, "Setting 2");
    // 可添加设置2的具体逻辑
}

void fun3_car_go(void) {
    ips200_show_string(30, 20, "Setting 3");
    // 可添加设置3的具体逻辑
}

void fun4_car_go(void) {
    ips200_show_string(30, 20, "Setting 4");
    // 可添加设置4的具体逻辑
}

void fun5_car_go(void) {
    ips200_show_string(30, 20, "Decrease Setting");
    // 可添加减小设置的具体逻辑
}

void fun6_car_go(void) {
    ips200_show_string(30, 20, "Increase Setting 1");
    // 可添加增加设置1的具体逻辑
}

void fun7_car_go(void) {
    ips200_show_string(30, 20, "Increase Setting 2");
    // 可添加增加设置2的具体逻辑
}


// 参数调节函数
void Adjust_zhi(int i) { pid.Kp+= 0.01; }
void Adjust_wan(void) { Speed_Goal += 2; }
void Adjust_minn(void) { pid.Kd += 0.01; }
void Adjust_zhi_(int i) { pid.Kp -= 0.01; }
void Adjust_wan_(void) { Speed_Goal -= 2; }
void Adjust_minn_(void) { pid.Kd -= 0.01; }
    